{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f46e0d75-b687-4ad7-b63b-1505affdbd18",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello there\n"
     ]
    }
   ],
   "source": [
    "print(\"hello there\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8211a315-9adb-4ad3-b8ec-6ac3685989a9",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'pygame'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[2], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mrandom\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpygame\u001b[39;00m\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'pygame'"
     ]
    }
   ],
   "source": [
    "import random\n",
    "import pygame\n",
    "import numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "3ab18b63-4ce9-4232-9f26-9259ccf7e86b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import carla"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a2f977a1-c57c-4430-83f1-2add855d2ae0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#connecting to the client and retrieve the world object\n",
    "client = carla.Client('localhost', 2000)\n",
    "world = client.get_world()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "867c3d8e-5915-49cd-9551-1635516c5a09",
   "metadata": {},
   "outputs": [],
   "source": [
    "#get spawn point\n",
    "spawn_points = world.get_map().get_spawn_points()\n",
    "\n",
    "#spawn the vehicle\n",
    "vehicle_bp = world.get_blueprint_library().filter('*firetruck*')\n",
    "#get the spawn point 0\n",
    "start_point = spawn_points[0]\n",
    "# try to spawn the vehicle on that spawn point\n",
    "vehicle = world.try_spawn_actor(vehicle_bp[0], start_point)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6c0a53f5-a75f-426b-9d98-2c1b6c5f4c7b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Transform(Location(x=-64.644836, y=24.472498, z=-0.001701), Rotation(pitch=0.000075, yaw=0.159202, roll=0.001821))\n"
     ]
    }
   ],
   "source": [
    "#print the vehicles postition on the map\n",
    "vehicle_pos = vehicle.get_transform()\n",
    "print(vehicle_pos)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "8de8095a-7b4a-441a-a484-5b2a66f6d922",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Transform(Location(x=-64.644844, y=24.471010, z=0.600000), Rotation(pitch=0.000000, yaw=0.159198, roll=0.000000))\n"
     ]
    }
   ],
   "source": [
    "#print the initial start point\n",
    "print(start_point)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "72bf903e-5ac6-4e4d-ae4d-054cb78ea211",
   "metadata": {},
   "outputs": [],
   "source": [
    "#send the NPC vehicle off\n",
    "vehicle.set_autopilot(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "927958a2-a058-440f-bec2-548e85bf5e63",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Transform(Location(x=-64.119507, y=24.432844, z=-0.005445), Rotation(pitch=0.147232, yaw=-0.640289, roll=0.071627))\n"
     ]
    }
   ],
   "source": [
    "#get the latest position of the vehicle\n",
    "vehicle_pos = vehicle.get_transform()\n",
    "print(vehicle_pos) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c86d7ec7-ac61-4fc3-892c-3bcd833a3a72",
   "metadata": {},
   "outputs": [],
   "source": [
    "#import route planner code from the simulation library path\n",
    "import sys\n",
    "sys.path.append('G:\\CARLA_0.9.14\\WindowsNoEditor\\PythonAPI\\carla')\n",
    "from agents.navigation.global_route_planner import GlobalRoutePlanner"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4e96c104-f51f-430e-8de4-b0cafba0b681",
   "metadata": {},
   "outputs": [],
   "source": [
    "#draw the routing path cpovered from point a to point b in the simluator\n",
    "\n",
    "sampling_resolution = 2\n",
    "\n",
    "grp = GlobalRoutePlanner(world.get_map(), sampling_resolution)\n",
    "\n",
    "point_a = carla.Location(x=-64.644844, y=24.471010, z=0.600000)\n",
    "point_b = carla.Location(x=-110.191475, y=-4.305392, z=-0.003566)\n",
    "\n",
    "#tracing the route\n",
    "route = grp.trace_route(point_a, point_b)\n",
    "\n",
    "#printing the route on map in the form of waypoints\n",
    "for waypoint in route:\n",
    "    world.debug.draw_string(waypoint[0].transform.location, 'O', draw_shadow=False, \n",
    "                            color = carla.Color(r=0, g=255, b=0), life_time = 120.0,\n",
    "                            persistent_lines = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "b54a318d-36be-4dc6-a865-dce0fae840c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#utility script for destruction, clear all the actors (basically undo or destroy the vehicle)\n",
    "\n",
    "for actor in world.get_actors().filter('*vehicle*'):\n",
    "    actor.destroy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "561b4fb2-e6cd-4864-a8a6-94edddabd9fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# bring the spectator position on the event point\n",
    "\n",
    "spectator = world.get_spectator()\n",
    "spawn_points = world.get_map().get_spawn_points()\n",
    "start_point = spawn_points[0]\n",
    "\n",
    "spectator_pos = carla.Transform(start_point.location + carla.Location(x=20, y=10, z=4), \n",
    "                                carla.Rotation(yaw = start_point.rotation.yaw -155))\n",
    "\n",
    "spectator.set_transform(spectator_pos)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "bc675156-37bc-462e-bbed-4292145f0619",
   "metadata": {},
   "outputs": [],
   "source": [
    "#drop the car called mini on top of the fire truck\n",
    "\n",
    "#first we define two cars\n",
    "truck_bp = world.get_blueprint_library().filter('*firetruck*')\n",
    "mini_bp = world.get_blueprint_library().filter('*cooper_s*')\n",
    "\n",
    "#spawn and start truck on start point\n",
    "truck = world.try_spawn_actor(truck_bp[0], start_point)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f95aa8b5-a4b3-4ced-ac7b-aa1dfb2312af",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "06f8a568-3b36-4a51-88f9-2fd0bbb6d135",
   "metadata": {},
   "outputs": [],
   "source": [
    "#first we spawn in somewhere else\n",
    "mini = world.try_spawn_actor(mini_bp[0], spawn_points[10])\n",
    "\n",
    "mini_pos = carla.Transform(start_point.location + carla.Location(x=-4, z=10),\n",
    "                           carla.Rotation(yaw = start_point.rotation.yaw - 0))\n",
    "\n",
    "mini.set_transform(mini_pos)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5fe217e0-2fcd-4f9e-953d-f9a436c3eb55",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77f88a45-d032-427a-bd6e-695a48cf5602",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c2debea-8bb3-463b-bf5a-69d06ca4dc72",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb3209e2-0926-433d-94d3-995a40035dfb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
